package edu.unc.comp410.colemangeer.ui;
//TODO: delete this class after I'm done testing with it

import javax.swing.*;
import javax.swing.event.*;

import edu.unc.comp410.colemangeer.system.UIController;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.*;

public class SpinnerTest extends JFrame {
	
	private UIController controller = new UIController();
	
	//TODO: change productList to productHT
	//declarations
	private static Hashtable productList = new Hashtable(); //initialize this to a very high prime number //hashtable has [productID, arrayOfDetails]
	private static Set htKeySet;
	private static JButton enterButton = new JButton( "Enter" );
	private EnterHandler enterHandler = new EnterHandler();
	private static Hashtable guiHT = new Hashtable();
	
	
	//main function
	public static void main(String args[]) {
		new SpinnerTest();
	}
	
	
	//constructor
	public SpinnerTest() {
		 super("JSpinner Test");
		 setSize(300,180);
		 setDefaultCloseOperation(EXIT_ON_CLOSE);
		 Container c = getContentPane();
		 c.setLayout(new GridLayout(0,2));
		 
		 //load the DB
		 fakeProductLoading();  //pretend to load the products from the DB
		 
		 
		 //loop throught the product hashtable to create a row for each product
		 for (Object i : htKeySet) {
			 	Integer currentProdID = (Integer)i; //the current key in the Hashtable is the product ID
			 	
			 	//put JSpinner into gui hashtable - this was the only way to dynamically name the Spinners
			 	guiHT.put(currentProdID, new JSpinner());  //this is so we can access the spinner.getValue()
			 	
			 	//current-vars
			 	ArrayList currentArray = (ArrayList)productList.get(currentProdID); //this array is the product details
			 	String currentLabel = currentArray.toString();						//this is the label to show on the swing pane
			 	JSpinner currentSpinner = (JSpinner)guiHT.get(currentProdID); 		//I drink coffee
			 	
			 	//place the stuff on the gui
			 	c.add( new JLabel( currentLabel ));
			 	c.add( currentSpinner );
		 }
		 
		 //buttons
		 c.add( new JLabel( "" ) );
		 enterButton.addActionListener( enterHandler );
		 c.add(enterButton);
		
		 setVisible(true);
	}//end constructor
	
	
	private class EnterHandler implements ActionListener {
		public void actionPerformed( ActionEvent e ) {
						
			//TODO: put try-catches in this gui code for bad entries or other errors
						
			//loop through the gui values and output them to screen
			for (Object i : htKeySet) {
			 	Integer currentProdID = (Integer)i;
			 	
			 	//current-vars  //*orange* maybe I should be looping through the guiHT instead of the productList, even though they should be the same cardinality
			 	ArrayList currentArray = (ArrayList)productList.get(currentProdID);
			 	String currentLabel = currentArray.toString();
			 	JSpinner currentSpinner = (JSpinner)guiHT.get(currentProdID);
			 	Integer currentSpinnerValue = (Integer)currentSpinner.getValue();
			 	String result = currentLabel + " " + currentSpinnerValue;
			 	
			 	System.out.println("HOORAY!! result is: " + result );
			}//end for-loop
		}//end actionperformed()
	} // End EnterHandler class.
	
	//this loads the productID and an array (holding product details) into a hashtable
	//the productID is the key and the array is the value
	//in the real code, this will have to pull the product details out of actual product objects
	public static void fakeProductLoading(){ 
		
			//key: productID, Value: array
			//array[0] is product ID, array[1] is description, array[2] is price
			List pDetails = new ArrayList();
			pDetails.add(0, 1);
			pDetails.add(1, "kyes book");
			pDetails.add(2, 39.99);
			productList.put(new Integer(1), pDetails ); 
			
			pDetails = new ArrayList();
			pDetails.add(0, 2);
			pDetails.add(1, "back scratcher");
			pDetails.add(2, 9.99);
			productList.put(new Integer(2), pDetails );
			
			pDetails = new ArrayList();
			pDetails.add(0, 3);
			pDetails.add(1, "monkey paw");
			pDetails.add(2, 100.00);
			productList.put(new Integer(3), pDetails );
			
			htKeySet = productList.keySet();			
	}//end fakeproductloading()
	
	
} //end SpinnerTest class
